package com.huadao.cart_ccs.common.timePicker;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.huadao.cart_ccs.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Yishuai on 2016/7/13.
 */
public class DateTimeWheelPicker {

    private Context context;
    private LayoutInflater inflater;
    private View view;
    private WheelView wv_year;
    private WheelView wv_month;
    private WheelView wv_day;
    private WheelView wv_hour;
    private WheelView wv_minute;
    private WheelView wv_second;

    private List<String> yearList;
    private int minYear=1990;
    private int maxYear=2050;
    private String current_year;
    private List<String> monthList;
    private String current_month;
    private List<String> dayList;
    private String current_day;

    private List<String> hourList;
    private String current_hour;
    private List<String> minuteList;
    private String current_minute;
    private List<String> secondList;
    private String current_second;

    private int offset=2;

    private CallBack callBack;
    public static SimpleDateFormat s2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Date defaultDate;
    public interface CallBack{
        void getDateTime(Date date);
    }

    public DateTimeWheelPicker(Context context, Date defaultDate) {
        this.context = context;
        this.defaultDate = defaultDate;
        Calendar cal = Calendar.getInstance();
        if(defaultDate!=null){
            cal.setTime(defaultDate);
        }
        view = LayoutInflater.from(context).inflate(R.layout.datetime_wheelpicker, null);

        wv_year= (WheelView) view.findViewById(R.id.wv_year);
        wv_month= (WheelView) view.findViewById(R.id.wv_month);
        wv_day= (WheelView) view.findViewById(R.id.wv_day);
        wv_hour= (WheelView) view.findViewById(R.id.wv_hour);
        wv_minute= (WheelView) view.findViewById(R.id.wv_minute);
        wv_second= (WheelView) view.findViewById(R.id.wv_second);

        initData();
        //年
        wv_year.setOffset(offset);
        wv_year.setItems(yearList);
        wv_year.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                current_year=item;
                if(current_month.equals("2月")){
                    int days=getDaysOfMonth("2月",current_year);
                    dayList.clear();
                    for(int i=1;i<days+1;i++){
                        dayList.add(i+"日");
                    }
                    wv_day.setItems(dayList);
                    wv_day.setSeletion(wv_day.getSeletedIndex()>dayList.size()-1?dayList.size()-1:wv_day.getSeletedIndex());
                    wv_day.invalidate();
                }

            }
        });
        if(defaultDate!=null){
            int defaultYear=cal.get(Calendar.YEAR);
            wv_year.setSeletion(yearList.indexOf(defaultYear+"年"));
        }

        current_year=wv_year.getSeletedItem();

        //月
        wv_month.setOffset(offset);
        wv_month.setItems(monthList);
        wv_month.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                current_month=item;
                int days=getDaysOfMonth(item,current_year);
                dayList.clear();
                for(int i=1;i<days+1;i++){
                    dayList.add(i+"日");
                }
                wv_day.setItems(dayList);
                wv_day.setSeletion(wv_day.getSeletedIndex()>dayList.size()-1?dayList.size()-1:wv_day.getSeletedIndex());
                wv_day.invalidate();
            }
        });
        if(defaultDate!=null){
            int defaultMonth=cal.get(Calendar.MONTH);
            wv_month.setSeletion(monthList.indexOf((defaultMonth+1)+"月"));
        }
        current_month=wv_month.getSeletedItem();

        //日
        wv_day.setOffset(offset);
        wv_day.setItems(dayList);
        wv_day.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                current_day=item;
            }
        });
        if(defaultDate!=null){
            int defaultDay=cal.get(Calendar.DAY_OF_MONTH);
            wv_day.setSeletion(dayList.indexOf(defaultDay+"日"));
        }
        current_day=wv_day.getSeletedItem();

        //时
        wv_hour.setOffset(offset);
        wv_hour.setItems(hourList);
        wv_hour.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                current_hour=item;
            }
        });
        if(defaultDate!=null){
            int defaultHour=cal.get(Calendar.HOUR_OF_DAY);
            wv_hour.setSeletion(hourList.indexOf(defaultHour+"时"));
        }
        current_hour=wv_hour.getSeletedItem();

        //分
        wv_minute.setOffset(offset);
        wv_minute.setItems(minuteList);
        wv_minute.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                current_minute=item;
            }
        });
        if(defaultDate!=null){
            int defaultMinute=cal.get(Calendar.MINUTE);
            wv_minute.setSeletion(minuteList.indexOf(defaultMinute+"分"));
        }
        current_minute=wv_minute.getSeletedItem();

        //秒
        wv_second.setOffset(offset);
        wv_second.setItems(secondList);
        wv_second.setOnWheelViewListener(new WheelView.OnWheelViewListener(){
            @Override
            public void onSelected(int selectedIndex, String item) {
                super.onSelected(selectedIndex, item);
                current_second=item;
            }
        });
        if(defaultDate!=null){
            int defaultSecond=cal.get(Calendar.SECOND);
            wv_second.setSeletion(secondList.indexOf(defaultSecond+"秒"));
        }
        current_second=wv_second.getSeletedItem();
    }

    private int getDaysOfMonth(String monthStr,String yearStr) {
        int month=Integer.parseInt(monthStr.substring(0,monthStr.length()-1));
        int year=Integer.parseInt(yearStr.substring(0,yearStr.length()-1));
        int days = 0;
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days=31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            case 2:
                if((year%100!=0)&&(year%4==0))
                    days=29;
                else if(year%400==0)
                    days=29;
                else if(year%3200==0&&year%172800==0)
                    days=29;
                else
                    days=28;
                break;
            default:break;
        }
        return days;
    }

    public void show(){
        AlertDialog dialog= new AlertDialog.Builder(context)
                .setTitle("select time:")
                .setView(view)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String dateStr=current_year.substring(0,current_year.length()-1)+"-"+current_month.substring(0,current_month.length()-1)+"-"+current_day.substring(0,current_day.length()-1)+" "+current_hour.substring(0,current_hour.length()-1)+":"+current_minute.substring(0,current_minute.length()-1)+":"+current_second.substring(0,current_second.length()-1);
                        try {
                            Date date= s2.parse(dateStr);
                            if(callBack!=null)
                                callBack.getDateTime(date);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                })
                .show();

        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.gravity= Gravity.CENTER;
        dialog.getWindow().setAttributes(lp);
    }

    private void initData() {
        if(yearList ==null)
            yearList =new ArrayList<String>();
        for(int i=minYear;i<maxYear;i++){
            yearList.add(i+"年");
        }

        if(monthList==null)
            monthList=new ArrayList<String>();
        for(int i=1;i<13;i++){
            monthList.add(i+"月");
        }

        if(dayList==null)
            dayList=new ArrayList<String>();
        int days=getDaysOfMonth((defaultDate.getMonth()+1)+"月",defaultDate.getYear()+"年");
        for(int i=1;i<days+1;i++){
            dayList.add(i+"日");
        }

        if(hourList==null)
            hourList=new ArrayList<String>();
        for(int i=0;i<24;i++){
            hourList.add(i+"时");
        }

        if(minuteList==null)
            minuteList=new ArrayList<String>();
        for(int i=0;i<60;i++){
            minuteList.add(i+"分");
        }

        if(secondList==null)
            secondList=new ArrayList<String>();
        for(int i=0;i<60;i++){
            secondList.add(i+"秒");
        }
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;

    }

    public WheelView getWv_year() {
        return wv_year;
    }

    public WheelView getWv_month() {
        return wv_month;
    }

    public WheelView getWv_day() {
        return wv_day;
    }

    public WheelView getWv_hour() {
        return wv_hour;
    }

    public WheelView getWv_minute() {
        return wv_minute;
    }

    public WheelView getWv_second() {
        return wv_second;
    }
}
